import { AEvent } from "../../../../../Core/Module/EventSystem/IEvent";
import { EventAttribute, GetParentTypeOf, GetTypeOf } from "../../../../../Decorator/Decorator";
import { SessionPlayerComponent } from "../../../../Model/Server/Demo/Gate/SessionPlayerComponent";
import { ActorLocationSenderComponent } from "../../../../Model/Server/Module/ActorLocation/ActorLocationSenderComponent";
import { NetServerComponentOnRead } from "../../../../Model/Server/Module/Message/NetServerComponent";
import { IActorRequest, IActorMessage } from "../../../../Model/Share/Module/Actor/IActorMessage";
import { IActorLocationRequest, IActorLocationMessage } from "../../../../Model/Share/Module/ActorLocation/IActorLocationMessage";
import { IResponse } from "../../../../Model/Share/Module/Message/IMessage";
import { MessageDispatcherComponent } from "../../../../Model/Share/Module/Message/MessageDispatcherComponent";

@EventAttribute(NetServerComponentOnRead)
export class NetServerComponentOnReadEvent extends AEvent<NetServerComponentOnRead> {

    public async Run(args: NetServerComponentOnRead) {
        // console.log(`NetServerComponentOnReadEvent.Run`);

        let session = args.Session;
        let message = args.Message;

        // console.log(GetTypeOf(message));
        // console.log(message);
        // console.log(GetTypeOf(IActorLocationRequest));
        // console.log(GetTypeOf(IActorLocationMessage));
        // console.log(GetTypeOf(IActorRequest));
        // console.log(GetTypeOf(IActorMessage));

        if (GetParentTypeOf(message) === GetTypeOf(IResponse)) {
            session.OnResponse(message as IResponse);
            return;
        }

        // 根据消息接口判断是不是Actor消息，不同的接口做不同的处理,比如需要转发给Chat Scene，可以做一个IChatMessage接口
        switch (GetParentTypeOf(message)) {
            case GetTypeOf(IActorLocationRequest): // gate session收到actor rpc消息，先向actor 发送rpc请求，再将请求结果返回客户端
                {
                    const actorLocationRequest = message as IActorLocationRequest;
                    const unitId = session.GetComponent<SessionPlayerComponent>(SessionPlayerComponent).PlayerId;
                    const rpcId = actorLocationRequest.RpcId; // 这里要保存客户端的rpcId
                    const instanceId = session.InstanceId;
                    const iResponse = await ActorLocationSenderComponent.Instance.Call(unitId, actorLocationRequest);
                    iResponse.RpcId = rpcId;
                    // session可能已经断开了，所以这里需要判断
                    if (session.InstanceId == instanceId) {
                        session.Send(iResponse);
                    }
                    break;
                }
            case GetTypeOf(IActorLocationMessage):
                {
                    const actorLocationMessage = message as IActorLocationMessage;
                    const unitId = session.GetComponent<SessionPlayerComponent>(SessionPlayerComponent).PlayerId;
                    ActorLocationSenderComponent.Instance.Send(unitId, actorLocationMessage);
                    break;
                }
            case GetTypeOf(IActorRequest):  // 分发IActorRequest消息，目前没有用到，需要的自己添加
                {
                    break;
                }
            case GetTypeOf(IActorMessage):  // 分发IActorMessage消息，目前没有用到，需要的自己添加
                {
                    break;
                }
            default:
                {
                    // 非Actor消息
                    MessageDispatcherComponent.Instance.Handle(session, message);
                    break;
                }
        }
    }

}